//
//  WNWelcomeViewController.swift
//  XLWeibo
//
//  Created by 风不会停息 on 2017/7/26.
//  Copyright © 2017年 风不会停息. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class WNWelcomeViewController: UIViewController {
    
    // MARK:----懒加载控件
    
    //背景图
    fileprivate lazy var bgImageView : UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    //用户头像
    fileprivate lazy var userImageView : UIImageView = {
        
        let imageView =  UIImageView(image: UIImage(named:"avatar_default_big"))

        if let imageUrl = WNUserAccountViewModel.sharedViewModel.userAccount?.avatar_large{
            
            print("--------------" + imageUrl)
            imageView.sd_setImage(with: URL(string:imageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        //圆角设置和多余部分裁剪
        imageView.layer.cornerRadius = 45
        
        imageView.layer.masksToBounds = true
        
        return imageView
    }()
    
     override func viewDidAppear(_ animated: Bool) {
        startAnimation()
    }
    
    //消息内容
    fileprivate lazy var messageLabel: UILabel = {
        let label = UILabel()
        if let name = WNUserAccountViewModel.sharedViewModel.userAccount?.name {
            label.text = "欢迎回来，\(name)"
        } else {
            label.text = "欢迎回来"
        }
        //  默认是透明
        label.alpha = 0
        //  设置字体大小
        label.font = UIFont.systemFont(ofSize: 12)
        //  文字颜色
        label.textColor = UIColor.darkGray
        return label
    }()
    
    override func loadView() {
        view = bgImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }

    //  添加控件设置约束
    private func setupUI() {
        view.addSubview(userImageView)
        view.addSubview(messageLabel)
        
        userImageView.snp.makeConstraints { (make) in
            make.top.equalTo(view).offset(200)
            make.centerX.equalTo(view)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        
        messageLabel.snp.makeConstraints { (make) in
            make.top.equalTo(userImageView.snp.bottom).offset(10)
            make.centerX.equalTo(userImageView)
        }
    }

    //头像动画
    private func startAnimation(){
        userImageView.snp.updateConstraints { (make) in
            
            make.top.equalTo(view).offset(100)
        }
        
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 10, options: [], animations: {
            
            self.view.layoutIfNeeded()

        }) { (_) in
            UIView.animate(withDuration: 1, animations: { 
                self.messageLabel.alpha = 1.0
            }, completion: { (_) in
                
                NotificationCenter.default.post(name: NSNotification.Name(SwitchRootVCNotificationName), object: nil)
            })
        }
        
    }
    
    
    
    
    
    
    
    
    
    
    
    

}
